package org.vectrics.recruiter.struts.tag;

import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.user.Interview;
import org.vectrics.recruiter.user.RecruitUserProvider;
import org.vectrics.user.PersonEncounterStatusType;
import org.apache.log4j.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;



public class InterviewResultComboTag extends TagSupport {
    static private Category log = Category.getInstance(InterviewResultComboTag.class);
    private String styleClass = null;
    private String property = null;
    private String value = null;
    

	public int doStartTag() throws JspException  {
		StringBuffer html = new StringBuffer();
		
		log.debug("doStartTag() called");
		HttpServletRequest request = (HttpServletRequest)super.pageContext.getRequest();
		String contextPath = request.getContextPath();
		
		String selectValue = value;
		if (selectValue == null)
			selectValue = "";
		log.debug("doStartTag() - selectValue = " + selectValue);
		
		//HttpSession session = super.pageContext.getSession();
		if (styleClass == null) {
			html.append("<select name=\"" + getProperty() +  "\" style=\"width:250;font-family:verdana,arial,sans serif;font-size=" +
					"11px\">");
		}
		else {
			html.append("<select name=\"" + getProperty() +  "\" class=\"" + styleClass + "\">");
		}
		
		Collection interviewStatusTypes = RecruitServiceLocator.getPersonService().findAllPersonEncounterStatusTypes();
		Iterator iter = interviewStatusTypes.iterator();
		while (iter.hasNext()) {
		    PersonEncounterStatusType statusType = (PersonEncounterStatusType)iter.next();
		    addOption(html, statusType.getCode(), statusType.getName(), selectValue);
		}
		
		html.append("</select>");

		
		JspWriter writer = pageContext.getOut();
		try {
			writer.print(html.toString());
		}
		catch (IOException ex) {
			throw new JspException("IOException attempting to write tag to output. " + ex.getMessage());
		}
		return(EVAL_PAGE);
	}

	public void addOption(StringBuffer html, String code, String description, String select) {
		String selectStr = "";
		if (select.equals(code)) {
			selectStr = " SELECTED";
		}
		html.append("<option value=\"" + code + "\"" + selectStr + ">" 
						+ description + "</option>");
	}

	/**
	 * @return
	 */
	public String getStyleClass() {
		return styleClass;
	}

	/**
	 * @param styleClass
	 */
	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	/**
	 * @return
	 */
	public String getProperty() {
		return property;
	}

	/**
	 * @param property
	 */
	public void setProperty(String property) {
		this.property = property;
	}

	/**
	 * @return
	 */
	public String getValue() {
		return value;
	}

	/**
	 * @param value
	 */
	public void setValue(String value) {
		this.value = value;
	}

}
